package cn.tedu.person.service.impl;

import cn.tedu.person.ex.ServiceException;
import cn.tedu.person.mapper.AdminMapper;
import cn.tedu.person.mapper.DepartmentMapper;
import cn.tedu.person.pojo.dto.AdminInsertDTO;
import cn.tedu.person.pojo.dto.AdminUpdateDTO;
import cn.tedu.person.pojo.dto.DepartmentInsertDTO;
import cn.tedu.person.pojo.dto.DepartmentUpdateDTO;
import cn.tedu.person.pojo.entity.Admin;
import cn.tedu.person.pojo.entity.Department;
import cn.tedu.person.pojo.vo.AdminIndexVO;
import cn.tedu.person.pojo.vo.AdminStandardVO;
import cn.tedu.person.pojo.vo.DepartmentIndexVO;
import cn.tedu.person.pojo.vo.DepartmentStandardVO;
import cn.tedu.person.service.IAdminService;
import cn.tedu.person.service.IDepartmentService;
import cn.tedu.person.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void addNew(DepartmentInsertDTO departmentInsertDTO) {

        String departmentName = departmentInsertDTO.getDepartment();
        int countByName = departmentMapper.countByName(departmentName);
        if (countByName > 0) {
            String message = "添加部门失败，名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Department department=new Department();
        BeanUtils.copyProperties(departmentInsertDTO,department);
        int rows = departmentMapper.insert(department);
        if (rows != 1) {
            String message = "添加部门失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public List<DepartmentIndexVO> list() {
        List<DepartmentIndexVO> list=departmentMapper.list();
        return list;
    }

    @Override
    public void delete(Long id) {
        DepartmentStandardVO queryResult = departmentMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "删除部门失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int rows = departmentMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除部门失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, DepartmentUpdateDTO departmentUpdateDTO) {
        log.debug("修改业务开始...{}",departmentUpdateDTO);
        DepartmentStandardVO queryResult = departmentMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "获取部门详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

//        String departmentName = departmentUpdateDTO.getDepartment();
//        int countByName = departmentMapper.countByName(departmentName);
//        if (countByName > 0) {
//            String message = "修改部门失败，名称已经被占用！";
//            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
//        }

        Department department=new Department();
        BeanUtils.copyProperties(departmentUpdateDTO,department);
        log.debug("department={}",department);
        int rows = departmentMapper.update(department);
        if (rows != 1) {
            String message = "修改部门失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

    }

    @Override
    public DepartmentStandardVO getStandardById(Long id) {
        DepartmentStandardVO queryResult = departmentMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "获取部门详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

}
